<?php

class ArticleApp extends MallbaseApp
{

    var $_article_mod;
    var $_acategory_mod;
    var $_ACC; //系统文章cate_id数据
    var $_cate_ids; //当前分类及子孙分类cate_id
    function __construct()
    {
        $this->ArticleApp();
    }
    function ArticleApp()
    {
        parent::__construct();
        $this->_article_mod = &m('article');
        $this->_acategory_mod = &m('acategory');
        /* 获得系统分类cate_id数据 */
        //$this->_ACC = $this->_acategory_mod->get_ACC();
    }
    function index()
    {
		$cate_id= isset($_GET['cate_id']) ? intval($_GET['cate_id']) : '0';
		//帮助中心的首页
		if (!$cate_id)
		{
			$search_q = isset($_GET['q']) ? preg_replace("/<(.*?)>/","",htmldecode($_GET['q'])) : '';
			if ($search_q)
			{
				//搜索结果
				$page = $this->_get_page(10);   //获取分页信息
				$conditions = " AND ( title LIKE '%{$search_q}%' OR content LIKE '%{$search_q}%')";
				$search_articles = $this->_article_mod->find(array(
					'conditions'  => 'if_show=1 AND store_id=0 AND code = ""' . $conditions,
					'limit'   => $page['limit'],
					'order'   => $sort_order,
					'count'   => true   //允许统计
				)); 
				$page['item_count'] = $this->_article_mod->getCount();	
				foreach ( $search_articles as $key => $lcjsearch)
				{
					$search_articles[$key]['content'] = sub_str(strip_tags($lcjsearch['content']), 200, true);
				}
				//print_r($search_articles);
				$this->_format_page($page);
				$this->assign('page_info', $page);
				$this->assign('search_articles', $search_articles);
				$this->assign('search_q', $search_q);	
				$this->_config_seo(array(
					'title'       => '搜索' . $search_q . '的答案 - ' . LANG::get('help_center') . ' - ' . Conf::get('site_name'),
					'keywords'    => '搜索' . $search_q . '的答案,' . LANG::get('help_center') . ',' . Conf::get('site_name'),
					'description' => '搜索' . $search_q . '的答案,' . LANG::get('help_center') . ',' . Conf::get('site_name')
				));				
			}else{			
                $this->_config_seo('title', LANG::get('help_center') . ' - ' . Conf::get('site_name'));	
			}
		}else{
				//分类页
				$this->_cate_ids = $cate_id;
				/* 分类下的所有文章 */
				$all = $this->_get_article('all');
				$articles = $all['articles'];
				$page = $all['page'];	
			
				// 页面标题
				$category = $this->_acategory_mod->get_info($cate_id);	
				$this->assign('articles', $articles);
				$this->assign('category', $category);
				$this->assign('new_articles', $new_articles);
				$this->_format_page($page);
				$this->assign('page_info', $page);		
				$this->_config_seo(array(
					'title'       => $category['cate_name'] . ' - ' . LANG::get('help_center') . ' - ' . Conf::get('site_name'),
					'keywords'    => $category['cate_name'] . ',' . LANG::get('help_center') . ',' . Conf::get('site_name'),
					'description' => $category['cate_name'] . ',' . LANG::get('help_center') . ',' . Conf::get('site_name')
				));										
		}
		//print_r($curlocal);
        $this->assign('acategories', $this->_get_by_lcjclass());
        $this->display('article.index.html');
    }
	/*============================公司网站的关于我们的页面调用==============================*/
    function abouts()
	{
        $now_code = empty($_GET['code']) ? 0 : htmldecode($_GET['code']);
        if (!$now_code)
        {	
			$this->show_warning('对不起，访问过程中遇到了问题');
			return;			
		}
		//横幅广告调用		
		$adver_list = $this->_get_flash_adver('77');
		$this->assign('adver_list', $adver_list[1]);
		
        //缓存数据调用
		$cache_server =& cache_server();		
		$key = 'abouts_get_list';
        $category_about = $cache_server->get($key);
        if ($category_about === false)
        {
			//调出所有公司内容列表
			$abouts_mod = &m('abouts');
			$category_about = $abouts_mod->find(array( 'order' => 'sort_order, cate_id' ));
			$cache_server->set($key, $category_about, 86400);
		}

		foreach ($category_about as $key => $for_about)
		{
			if ($now_code == $for_about['code']){
				$now_about= array('title' => $for_about['cate_name'], 'content'=> $for_about['content'], 'code'=> $for_about['code'], 'parent_id'=> $for_about['parent_id']);
				$location[1]= array('text' => $for_about['cate_name'], 'url'=> url('app=article&act=abouts&code=' . $for_about['code']), 'cate_id'=> $for_about['cate_id'], 'code'=> $for_about['code']);
			}
		}
		$category = array();
		foreach ($category_about as $key => $for_about)
		{
			if ($for_about['parent_id']>0)
			{
				$category[$for_about['parent_id']]['list'][] = $for_about;
			}else{
				$category[$key] = $for_about;
			}
			if ($now_about['parent_id'] == $for_about['cate_id']){
				$location[0]= array('text' => $for_about['cate_name'], 'url'=> url('app=article&act=abouts&code=' . $for_about['code']), 'cate_id'=> $for_about['cate_id'], 'code'=> $for_about['code']);
			}						
		}
		if(!$now_about){
            $this->show_warning('没有找到相关内容！');
            return;
		}
		
        /* 当前位置 */
		if(count($location)>1)//数组排序
		{ 
		   ksort($location);
	    }else{
		  $location && $location=array_values($location);
	    }
		$this->_curlocal($location);
		$this->assign('location', $location);
		
		$this->assign('now_about', $now_about);
		$this->assign('category_about', $category);
		$this->assign('now_current', $location[0]["code"]);//赋值导航条当前位置
		$this->_config_seo(array(
            'title' => $now_about['title'] . ' - ' . Conf::get('site_name'),
        ));		
		$this->display('about.content.html');
	}
	//调出频道的切换广告图片
	function _get_flash_adver($flash_id)
	{
		$this->adver_mod =& m('adver');
		$adver_list = $this->adver_mod->_get_adver_list($flash_id);
		$adver_list[$flash_id]['ad_img_name'] = unserialize($adver_list[$flash_id]['ad_img_name']);
		$adver_list[$flash_id]['ad_link'] = unserialize($adver_list[$flash_id]['ad_link']);
		$adver_list[$flash_id]['ad_img'] = unserialize($adver_list[$flash_id]['ad_img']);		
		$adver_list['flash']=array();$i=1;
		foreach($adver_list[$flash_id]['ad_img'] as $key => $var) 
		{
			$adver_list['flash'][$i]['ad_img_name'] = $adver_list[$flash_id]['ad_img_name'][$key];
			$adver_list['flash'][$i]['ad_link'] = $adver_list[$flash_id]['ad_link'][$key];
			$adver_list['flash'][$i]['ad_img'] = $var;
			$i++;
		}
		return $adver_list['flash'];
	}
    function abouts_view()
    {
        $article_id = empty($_GET['article_id']) ? 0 : intval($_GET['article_id']);
        if ($article_id>0)
        {
            $article = $this->_article_mod->get('article_id=' . $article_id . ' AND code = "" AND if_show=1 AND store_id=0');
            if (!$article)
            {
                $this->show_warning('no_such_article');
                return;
            }
            if ($article['link']){ //外链文章跳转
                header("HTTP/1.1 301 Moved Permanently");
                header('location:'.$article['link']);
                return;
            }
        }

		//调出新闻公告
		$this->_cate_ids = 2;
		$ganggao = $this->_get_article("new");
		$ganggao["articles"] && $ganggao["articles"]=array_values($ganggao["articles"]);//重置键值
		$page = $ganggao['page'];	
		$this->_format_page($page);
		$this->assign('page_info', $page);	
		$this->assign('ganggao', $ganggao);
		//print_r($ganggao.articles);
		//默认性显示文章
		if(!$article['article_id'] && !$article['title']){
			$article['article_id']=$ganggao["articles"][0]['article_id'];
			$article['title']=$ganggao["articles"][0]['title'];
			$article['content']=$ganggao["articles"][0]['content'];
		}
		$this->assign('article', $article);
		
		//横幅广告调用		
		$adver_list = $this->_get_flash_adver('77');
		$this->assign('adver_list', $adver_list[1]);
				
        $this->_config_seo(array(
            'title'       => $article['title'] . ' - 新闻公告 - ' . Conf::get('site_name'),
            'keywords'    => $article['title'] . ',新闻公告,' . Conf::get('site_name'),
            'description' => $article['title'] . ',新闻公告,' . Conf::get('site_name')
        ));

		$this->display('about.view.html');
    }
	/*============================公司网站的关于我们的页面调用 End==============================*/

    function view()
    {
        $article_id = empty($_GET['article_id']) ? 0 : intval($_GET['article_id']);
        $cate_ids = array();
        if ($article_id>0)
        {
            $article = $this->_article_mod->get('article_id=' . $article_id . ' AND code = "" AND if_show=1 AND store_id=0');
            if (!$article)
            {
                $this->show_warning('no_such_article');
                return;
            }
            if ($article['link']){ //外链文章跳转
                header("HTTP/1.1 301 Moved Permanently");
                header('location:'.$article['link']);
                return;
            }
            /* 上一篇下一篇 */
            $pre_article = $this->_article_mod->get('cate_id = '.$article['cate_id'].' AND article_id<' . $article_id . ' AND code = "" AND if_show=1  AND store_id=0 ORDER BY article_id DESC limit 1');
            $pre_article && $pre_article['target'] = $pre_article['link'] ? '_blank' : '_self';
            $next_article = $this->_article_mod->get('cate_id = '.$article['cate_id'].' AND article_id>' . $article_id . ' AND code = "" AND if_show=1  AND store_id=0 ORDER BY article_id ASC limit 1');
            $next_article && $next_article['target'] = $next_article['link'] ? '_blank' : '_self';
            if ($article)
            {
                $cate_id = $article['cate_id'];
                /* 取得当前分类及子孙分类cate_id */
                $cate_ids = $this->_acategory_mod->get_descendant($cate_id);
            }
            else
            {
                $this->show_warning('no_such_article');
                return;
            }
        }
        else
        {
            $this->show_warning('no_such_article');
            return;
        }

        $this->_cate_ids = $cate_ids;
        /* 当前位置 */
        $curlocal = $this->_get_article_curlocal($cate_id);
        //$this->_curlocal($curlocal);
        /* 新文章 */
        $new = $this->_get_article('new');
        $new_articles = $new['articles'];
        $this->assign('curlocal', $curlocal);
		$this->assign('article', $article);
        $this->assign('pre_article', $pre_article);
        $this->assign('next_article', $next_article);
        $this->assign('new_articles', $new_articles);
		$this->assign('acategories', $this->_get_by_lcjclass());

        $this->_config_seo(array(
            'title'       => $article['title'] . ' - ' . LANG::get('help_center') . ' - ' . Conf::get('site_name'),
            'keywords'    => $article['title'] . ',' . LANG::get('help_center') . ',' . Conf::get('site_name'),
            'description' => $article['title'] . ',' . LANG::get('help_center') . ',' . Conf::get('site_name')
        ));

		$this->display('article.view.html');
    }
	//关于我们的页面调用
    function about()
	{
        $now_code = empty($_GET['code']) ? 0 : htmldecode($_GET['code']);
        if (!$now_code)
        {	
			$this->show_warning('对不起，访问过程中遇到了问题');
			return;			
		}
		$category_about = $this->_article_mod->find(array(
			'conditions' => "store_id=0 AND cate_id=1",
			'order' => 'sort_order, cate_id',
		));
		foreach ($category_about as $key => $for_about)
		{
			if ($now_code == $for_about['code'])
			{
				$now_about= array('title' => $for_about['title'], 'content'=> $for_about['content'], 'code'=> $for_about['code']);
			}
		}
		//print_r($now_about);
		$this->assign('now_about', $now_about);
		$this->assign('category_about', $category_about);
        /* 当前位置 */
        $curlocal = array(array('text' => $now_about['title'],'url' => 'pifa_'.$now_about['code'].'.html' ));	
		$this->_curlocal($curlocal);
		$this->_config_seo(array(
            'title'       => $now_about['title'] . ' - ' . Conf::get('site_name'),
            'keywords'    => $now_about['title'] . ',' . Conf::get('site_name'),
            'description' => $now_about['title'] . ',' . Conf::get('site_name')
        ));		
		$this->display('article.about.html');
	}
	function system()
    {
        $code = empty($_GET['code']) ? '' : htmldecode($_GET['code']);
        if (!$code)
        {
            $this->show_warning('no_such_article');
            return;
        }
        $article = $this->_article_mod->get("code='" . $code . "'");
        if (!$article)
        {
            $this->show_warning('no_such_article');
            return;
        }
        if ($article['link']){ //外链文章跳转
                header("HTTP/1.1 301 Moved Permanently");
                header('location:'.$article['link']);
                return;
            }

        /*当前位置*/
        $curlocal[] =array('text' => $article['title']);
        $this->_curlocal($curlocal);
        $this->assign('article', $article);
		$this->_config_seo(array(
			'title'       => $article['title'] . ' - ' . Conf::get('site_name'),
			'keywords'    => $article['title'] . ',' . Conf::get('site_name'),
			'description' => $article['title'] . ',' . Conf::get('site_name')
		));				
        $this->display('article.view1000.html');

    }
    function _get_article_curlocal($cate_id)
    {
        $parents = array();
        if ($cate_id)
        {
            $acategory_mod = &m('acategory');
            $acategory_mod->get_parents($parents, $cate_id);
        }
        foreach ($parents as $category)
        {
            $curlocal[] = array('text' => $category['cate_name'], 'ACC' => $category['code'], 'url' => 'index.php?app=article&amp;cate_id=' . $category['cate_id']);
        }
        return $curlocal;
    }
    function _get_acategory($cate_id)
    {
        $acategories = $this->_acategory_mod->get_list($cate_id);
        if ($acategories){
            unset($acategories[$this->_ACC[ACC_SYSTEM]]);
            return $acategories;
        }
        else
        {
            $parent = $this->_acategory_mod->get($cate_id);
            if (isset($parent['parent_id']))
            {
                return $this->_get_acategory($parent['parent_id']);
            }
        }
    }
    function _get_article($type='')
    {
        $conditions = '';
        $per = '';
        switch ($type)
        {
            case 'new' : $sort_order = 'add_time DESC,sort_order ASC';
            $per=10;
            break;
            case 'all' : $sort_order = 'sort_order ASC,add_time DESC';
            $per=25;
            break;
        }
        $page = $this->_get_page($per);   //获取分页信息
        !empty($this->_cate_ids)&& $conditions = ' AND cate_id ' . db_create_in($this->_cate_ids);		
        $articles = $this->_article_mod->find(array(
            'conditions'  => 'if_show=1 AND store_id=0 AND code = ""' . $conditions,
            'limit'   => $page['limit'],
            'order'   => $sort_order,
            'count'   => true   //允许统计
        )); //找出所有符合条件的文章
        $page['item_count'] = $this->_article_mod->getCount();
        foreach ($articles as $key => $article)
        {
            $articles[$key]['target'] = $article[link] ? '_blank' : '_self';
        }		
        return array('page'=>$page, 'articles'=>$articles);
    }
	function _get_by_lcjclass()
	{
        //只显示帮助中心，除却code的值是空的话，才显示出来的
        $acategories = $this->_acategory_mod->find(array(
            'conditions' => "code IS NULL ",
            'order' => 'sort_order, cate_id',
        ));
		//功能：拆开分类，自动分成无限级别的分类
		import('tree.lib');
		$tree = new Tree();
		$tree->setTree($acategories, 'cate_id', 'parent_id', 'cate_name');
		$data = $tree->getArrayList(0);	
		return $data;
	}
}
?>